package com.longxi.tianjintianbao.ui.adapter.multitype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SelectDeletePersonnelListener;
import com.longxi.tianjintianbao.model.Personnel;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class AddPersonnelListViewBinder extends ItemViewBinder<Personnel, AddPersonnelListViewBinder.ViewHolder> {

    SelectDeletePersonnelListener listener;

    public AddPersonnelListViewBinder(SelectDeletePersonnelListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_add_personnel, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Personnel personnel) {

        if("".equals(personnel.getUserid()) || "".equals(personnel.getUsername())){
            holder.mIvHead.setImageResource(R.mipmap.ic_add_personnel);
            holder.mTvName.setText("");
            holder.mLlBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onSuccess(null);
                }
            });
            holder.mIvDelete.setVisibility(View.GONE);
        }else{
            holder.mIvHead.setImageResource(R.mipmap.ic_no_head);
            holder.mTvName.setText(personnel.getDept() + "\n" + personnel.getUsername());
            holder.mIvDelete.setVisibility(View.VISIBLE);
            holder.mIvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onSuccess(personnel);
                }
            });
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mLlBtn;
        ImageView mIvHead;
        TextView mTvName;
        ImageView mIvDelete;
        ViewHolder(View itemView) {
            super(itemView);
            mLlBtn = itemView.findViewById(R.id.llBtn);
            mIvHead = itemView.findViewById(R.id.ivHead);
            mTvName = itemView.findViewById(R.id.tvName);
            mIvDelete = itemView.findViewById(R.id.ivDelete);
        }
    }
}
